package com.sluggard.workflow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sluggard.common.exception.BusinessException;
import com.sluggard.common.model.res.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.task.TaskQuery;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Tag(name="工作流-任务管理")
@RestController
@RequestMapping("workflow/runtime/task")
public class TaskController {

    @Resource
    private TaskService taskService;

    @Operation(summary = "查询任务列表")
    @GetMapping("/list")
    public Result<Page<Task>> list(
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "assignee", required = false) String assignee) {
        
        TaskQuery query = taskService.createTaskQuery();
        if (StringUtils.isNotBlank(assignee)) {
            query.taskAssignee(assignee);
        }
        
        List<Task> tasks = query
                .orderByTaskCreateTime().desc()
                .listPage((pageNo - 1) * pageSize, pageSize);

        Page<Task> page = new Page<>();
        page.setRecords(tasks);
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        page.setTotal(query.count());
        
        return Result.ok(page);
    }

    @Operation(summary = "完成任务")
    @PostMapping("/{taskId}/complete")
    public Result<Void> completeTask(
            @PathVariable String taskId,
            @RequestBody(required = false) Map<String, Object> variables) {
        try {
            if (variables != null) {
                taskService.complete(taskId, variables);
            } else {
                taskService.complete(taskId);
            }
            return Result.ok();
        } catch (Exception e) {
            log.error("完成任务失败", e);
            throw new BusinessException("完成任务失败");
        }
    }

    @Operation(summary = "委派任务")
    @PostMapping("/{taskId}/delegate")
    public Result<Void> delegateTask(
            @PathVariable String taskId,
            @RequestParam String userId) {
        try {
            taskService.delegateTask(taskId, userId);
            return Result.ok();
        } catch (Exception e) {
            log.error("委派任务失败", e);
            throw new BusinessException("委派任务失败");
        }
    }

    @Operation(summary = "认领任务")
    @PostMapping("/{taskId}/claim")
    public Result<Void> claimTask(
            @PathVariable String taskId,
            @RequestParam String userId) {
        try {
            taskService.claim(taskId, userId);
            return Result.ok();
        } catch (Exception e) {
            log.error("认领任务失败", e);
            throw new BusinessException("认领任务失败");
        }
    }

//    @Operation(summary = "取消认领")
//    @PostMapping("/{taskId}/unclaim")
//    public Result<Void> unclaimTask(@PathVariable String taskId) {
//        try {
//            taskService.
//            return Result.ok();
//        } catch (Exception e) {
//            log.error("取消认领失败", e);
//            throw new BusinessException("取消认领失败");
//        }
//    }
} 